package com.example.eventrun.slice;

import com.example.eventrun.ResourceTable;
import ohos.agp.components.*;
import ohos.app.Context;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

public class item_listcontainer extends BaseItemProvider {
    List<date.ResultDTOX.ResultDTO.ListDTO> list;
    Context context;

    public item_listcontainer(List<date.ResultDTOX.ResultDTO.ListDTO> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        Component pare;
        date.ResultDTOX.ResultDTO.ListDTO date=list.get(i);
        if (component!=null){
            pare=component;
        }else {
            pare= LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item,null,false);
        }
        Text text= (Text) pare.findComponentById(ResourceTable.Id_text);
        Image image= (Image) pare.findComponentById(ResourceTable.Id_image);
        HttpURLConnection   connection = null;
        try {
            URL url = new URL(date.pic);
            URLConnection urlConnection =   url.openConnection();
            if (urlConnection instanceof   HttpURLConnection) {
                connection =   (HttpURLConnection) urlConnection;
            }
            if (connection != null) {
                connection.connect();
                // 之后可进行url的其他操作
                // 得到服务器返回过来的流对象
                InputStream inputStream =   urlConnection.getInputStream();
                ImageSource imageSource = ImageSource.create(inputStream,   new ImageSource.SourceOptions());
                ImageSource.DecodingOptions   decodingOptions = new ImageSource.DecodingOptions();
                decodingOptions.desiredPixelFormat   = PixelFormat.ARGB_8888;
                // 普通解码叠加旋转、缩放、裁剪
                PixelMap pixelMap = imageSource.createPixelmap(decodingOptions);
                // 普通解码
                context.getUITaskDispatcher().syncDispatch(()   -> {
                    Image image_2 = new   Image(context);
                    DirectionalLayout.LayoutConfig   config =
                            new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_CONTENT,
                                    DirectionalLayout.LayoutConfig.MATCH_CONTENT);
                    config.setMargins(10, 10,   10, 10);
                    image.setLayoutConfig(config);
                    image.setPixelMap(pixelMap);
//                    addComponent(image);
                    pixelMap.release();

                });
            }
        }   catch (Exception e) {
            e.printStackTrace();
        }
        text.setText(date.title);
        return pare;
    }
}
